<?php 
/*
 module:		日志管理控制器
 create_time:	2022-08-09 17:31:59
 author:		
 contact:		
*/

namespace app\Controller\admin;
use function Hyperf\ViewEngine\view;
use App\Exception\ValidateException;
use App\Model\admin\LogModel;
use App\Controller\admin\CommonController;
use Hyperf\DbConnection\Db;

class LogController extends CommonController {

    
    function t(){
        logger()->info('heyingmin');
        return 'abc';
    }
    
	/*
 	* @Description  数据列表
 	*/
	function index(){
		if($this->request->isMethod('get')){
			return view('admin/log/index');
		}else{
			$limit  = $this->request->input('limit', 20);
			$page = $this->request->input('page', 1);

			$order  = $this->request->input('order');	//排序方式
			$sort  = $this->request->input('sort');		//排序字段

			$where = [];
			$where['id'] = $this->request->input('id');
			$where['application_name'] = $this->request->input('application_name');
			$where['type'] = $this->request->input('type');
			$where['username'] = $this->request->input('username');

			$create_time = $this->request->input('create_time');
			if(isset($create_time[0]) && isset($create_time[1])){
				$where['create_time'] = ['between',[strtotime($create_time[0]),strtotime($create_time[1])]];
			}

			$sort = !empty($sort) ? $sort : 'id';
			$order = !empty($order) ? $order : 'desc';

			$field = 'id,application_name,type,username,url,ip,create_time';

			$query = LogModel::select(explode(",",$field));

			$res = formatWhere($query,$where)->orderBy($sort,$order)->paginate($limit)->toArray();

			$data['status'] = 200;
			$data['data'] = $res;
			$page == 1 && $data['sql_field_data'] = $this->getSqlField('application_name');
			return json($data);
		}
	}


	/*
 	* @Description  删除
 	*/
	function delete(){
		$idx =  $this->request->input('id');
		if(!$idx) throw new ValidateException ('参数错误');
		LogModel::whereIn('id',explode(',',$idx))->delete();
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/*
 	* @Description  查看详情
 	*/
	function detail(){
		$id =  $this->request->input('id');
		if(!$id) throw new ValidateException ('参数错误');

		$field = 'id,application_name,type,username,url,ip,useragent,content,errmsg,create_time';

		$res = LogModel::select(explode(",",$field))->find($id);
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  导出
 	*/
	function dumpdata(){
		$page = $this->request->input('page', 1);
		$limit = config('my.dumpsize') ? config('my.dumpsize') : 1000;

		$state = $this->request->input('state');
		$where = [];
		$where['id'] = ['in',$this->request->input('id')];
		$where['application_name'] = $this->request->input('application_name');
		$where['type'] = $this->request->input('type');
		$where['username'] = $this->request->input('username');

		$create_time = $this->request->input('create_time');
		if(isset($create_time[0]) && isset($create_time[1])){
			$where['create_time'] = ['between',[strtotime($create_time[0]),strtotime($create_time[1])]];
		}

		$order  = $this->request->input('order');	//排序方式
		$sort  = $this->request->input('sort');		//排序字段

		$sort = !empty($sort) ? $sort : 'id';
		$order = !empty($order) ? $order : 'desc';

		$field = 'application_name,type,username,url,ip,useragent,content,errmsg,create_time';

		$query = LogModel::select(explode(",",$field));

		$res = formatWhere($query,$where)->orderBy($sort,$order)->paginate($limit)->toArray();

		foreach($res['data'] as $key=>$val){
			$res['data'][$key]['type'] = getItemVal($val['type'],'[{"key":"登录日志","val":"1","label_color":"info"},{"key":"操作日志","val":"2","label_color":"warning"},{"key":"异常日志","val":"3","label_color":"danger"}]');
			$res['data'][$key]['create_time'] = !empty($val['create_time']) ? date('Y-m-d H:i:s',$val['create_time']) : '';
			unset($res['data'][$key]['id']);
		}

		$data['status'] = 200;
		$data['header'] = explode(',','所属应用,日志类型,用户名,请求url,客户端ip,浏览器信息,请求内容,异常信息,创建时间');
		$data['percentage'] = ceil($page * 100/ceil($res['total']/$limit));
		$data['filename'] = '日志管理.'.config('my.dump_extension');
		$data['data'] = $res['data'];
		return json($data);
	}


	/*
 	* @Description  获取定义sql语句的字段信息
 	*/
	function getFieldList(){
		return json(['status'=>200,'data'=>$this->getSqlField('application_name')]);
	}

	/*
 	* @Description  获取定义sql语句的字段信息
 	*/
	private function getSqlField($list){
		$data = [];
		if(in_array('application_name',explode(',',$list))){
			$data['application_names'] = $this->query('select app_dir,application_name from pre_application','default');
		}
		return $data;
	}





}



